﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HZP.CKGL
{
    public partial class GDXSDFRM: Form
    {
        private BindingSource bsList = new BindingSource();
        public GDXSDFRM()
        {
            InitializeComponent();

            UTIL.InitDGVColumns(dgvList, new ColConfig[]
           {
                new ColConfig {ColName="日期",FieldName="RQ",Editable=true,Width=150 },
                new ColConfig {ColName="工地名称",FieldName="GDID",Editable=true ,ColType="combo" ,Width=160},
                new ColConfig {ColName="产品",FieldName="CPID",Editable=true,ColType="combo",Width=160 },
                new ColConfig {ColName="数量",FieldName="SL",Editable=true },
                new ColConfig {ColName="总价",FieldName="ZJ",Editable=false },
                new ColConfig {ColName="已付款",FieldName="YFK",Editable=true },
                new ColConfig {ColName="未付款",FieldName="WFK",Editable=true },
                new ColConfig {ColName="备注",FieldName="BZ",Editable=true,Width=200 }
           });
            dtimeStart.Value = DateTime.Today;
            dtimeEnd.Value = DateTime.Today.AddDays(1);

            dgvList.DataSource = bsList;
            dgvList.UserAddedRow += (object sender, DataGridViewRowEventArgs e) => dgvList.Rows[e.Row.Index - 1].Cells["RQ"].Value = DateTime.Now;
            dgvList.DataError += (object sender, DataGridViewDataErrorEventArgs e) => { };
            showList();

            btnQuery.Click += btnQuery_Click;
            btnSave.Click += btnSave_Click;
            btnDel.Click += btnDel_Click;
        }

        private void showList()
        {
            UTIL.BindComboColumn(dgvList.Columns["GDID"] as DataGridViewComboBoxColumn,
                DB.CKGL.Query("select GDname+'-'+GDlxr mc,ID from GDINFO ORDER BY PX"));
            UTIL.BindComboColumn(dgvList.Columns["CPID"] as DataGridViewComboBoxColumn,
                DB.CKGL.Query("select cpmc+'-'+cpxh mc,ID from cpinfo ORDER BY PX"));

            //绑定数据
            string start = dtimeStart.Value.ToString("yyyy-MM-dd");
            string end = dtimeEnd.Value.ToString("yyyy-MM-dd");
            bsList.DataSource = DB.CKGL.Query("select * from xsd where rq>='" + start + "' and rq<='" + end + "' order by rq");
        }
        private void save()
        {
            DataTable dt = bsList.DataSource as DataTable;
            DB.CKGL.Update(dt, "select * from xsd where 1=2");
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            showList();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            save();
            showList();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除记录吗？ 删除后便无法恢复！") == DialogResult.OK)
            {
                foreach (DataGridViewRow select in dgvList.SelectedRows)
                {
                    dgvList.Rows.Remove(select);
                }
                btnSave.PerformClick();
            }
        }
    }
}
